<?php
/**
 * Retourne un Widget JQuery pour selectionner une date
 *
 * Options :
 *
 * - dateFormat : <ISO 8601 : 1979-04-07> Format de la date (Donnée transmise)
 * - userDateFormat : Format de la date affichée
 * - minDate: Date minimum
 * - maxDate: Date Maximum
 *
 * @author    Jacksay14
 * @package   widget
 * @since     2011
 */
class jckWidgetFormJQueryDateField extends sfWidgetFormInputText
{
  protected function configure($options = array(), $attributes = array()) {
    parent::configure($options, $attributes);


    $this->addOption('dateFormat', 'yy-mm-dd');
    $this->addOption('altFormat', 'DD d MM yy');
    $this->addOption('minDate', 'null');
    $this->addOption('maxDate', 'null');

    $user_lang = explode('_', sfContext::getInstance()->getUser()->getCulture());
    $this->addOption('regional', $user_lang[0]);

  }



  public function render($name, $value = null, $attributes = array(), $errors = array())
  {

    $html = '
      {input}
      <script type="text/javascript">
      $($(document).ready(function(){
        
        $("#{id}").datepicker({
          dateFormat: "{dateFormat}",
          regional: "{regional}",
          minDate:  {minDate},
          maxDate:  {maxDate},
          altFormat: "{altFormat}"
        });
        
        datePickerFriendly("{id}");
      }));
      </script>
    ';

    $template_vars = array(
        '{id}'    => $this->generateId($name),
        '{value}' => $value,
        '{dateFormat}' => $this->getOption('dateFormat'),
        '{regional}' => $this->getOption('regional'),
        '{minDate}' => $this->getOption('minDate'),
        '{maxDate}' => $this->getOption('maxDate'),
        '{altField}' => $this->getOption('altField'),
        '{altFormat}' => $this->getOption('altFormat'),
        '{input}' => parent::render($name, $value, $attributes, $errors)
    );

    return strtr($html, $template_vars);

  }




  /**
   *
   * @return <Array>
   */
  public function getJavaScripts() {
    return array_merge(
      array(
        sfConfig::get('app_jquery_js'),
        sfConfig::get('app_jquery_ui_js'),
        sfConfig::get('app_jquery_ui_dev_bundle_dir').'/ui/i18n/jquery.ui.datepicker-'.$this->getOption('regional').'.js',
        '/jckJQueryWidgetPlugin/js/datepicker_friendly.js'
      ),
      parent::getJavaScripts()
    );
  }



  /**
   *
   * @return <Array>
   */
  public function  getStylesheets() {
    return array_merge(
      parent::getStylesheets(),
      array(
          sfConfig::get('app_jquery_ui_css') => 'screen'
      )
    );
  }
}
